package org.molamil.acto.types {
	import org.molamil.acto.core.parsers.ParserError;	
	
	/**
	 * Type parser expecting the value passed when <code>parse()</code> to be 
	 * "Boolean compatible". These values are "true" and "false" (case insensitive).
	 * 
	 * <p>Note that this is a more strict implementation than the one used
	 * for <code>Boolean()</code> castings.</p>
	 * 
	 * @example The following code will parse "true" into <code>true</code>
	 * and will trace "HOLLY COW, I parsed a Boolean!!!":
	 * <listing version="3.0" >
	 * var parser:AbstractTypeParser = new BooleanTypeParser();
	 * var b:Boolean = parser.parse("true");
	 * if (b)
	 *    trace("HOLLY COW, I parsed a Boolean!!!");
	 * </listing>
	 * 
	 * @author Jorge Hernandez
	 */
	public class BooleanTypeParser extends AbstractTypeParser {
		
		
		// CONSTANTS
		
		/**
		 * @inheritDoc
		 */
		public static const PRIORITY:uint = 500;
		
		/**
		 * @inheritDoc
		 */
		public static const TYPE:Class = Boolean;
		
		
		// PROTECTED METHODS
		
		/**
		 * Returns the boolean representation of the passed value, or throws
		 * an <code>Error</code> if value does not contain any representation that 
		 * can be parsed into a <code>Boolean</code>
		 * 
		 * <p>Note that this is a more strict implementation than the one used
		 * for <code>Boolean()</code> castings.</p>
		 * 
		 * <p>The following strings will parse into a <code>Boolean</code></p>
		 * <ul>
		 * 	<li>"true" will parse into <code>true</code> (case insensitive)</li>
		 * 	<li>"false" will parse into <code>false</code> (case insensitive)</li>
		 * </ul>
		 * 
		 * @param value <code>String</code> value to test
		 * @return <code>Boolean</code> representation of the value passed
		 * @throws org.molamil.acto.core.errors.ParserError The value can't
		 * 	be converted to <code>Boolean</code>
		 */
		override protected function doParse(value:String):* {
			
			if (value == null)
				throw new ParserError("ERROR: value is null and cannot be parsed " +
					"into a Boolean");
			
			var v:String = value.toLowerCase();
			
			if (v == "true") {
				return true;
			} else if (v == "1") {
				return true;
			} else if (v == "false") {
				return false;
			} else if (v == "0") {
				return false;
			} else {
				throw new ParserError("ERROR: '" + value + "' cannot be parsed " +
					"into a Boolean");
			}
			
		}

		
	}
	
}